package com.lbs.tdrive.activity;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.ClipData.Item;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lbs.tdrive.comon.HttpASync;
import com.lbs.tdrive.comon.HttpASync.OnRequest;
import com.lbs.tdrive.constant.AppConstant;
import com.lbs.tdrive.data.Vehicle;
import com.lbs.tdrive.R;



public class AddVehicle extends Activity implements AdapterView.OnItemSelectedListener, OnClickListener, OnRequest{
	
	
	private Button addvehicle;
	private EditText mileage;
	private EditText vehiclename;
	private Context ctx;
	private String[] model={"1999","2000","2001","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012","2013","2014"};
	private String[] type={"Petrol","Diesel"};
	
	String modelStr;
	String typeStr;
	
	 protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.add_vehicle);
	        
	        Spinner spin_model=(Spinner)findViewById(R.id.spinner1);
	        Spinner spin_type=(Spinner)findViewById(R.id.spinner2);
	        spin_model.setOnItemSelectedListener(this);
	        spin_type.setOnItemSelectedListener(this);
	        ArrayAdapter<String> model_list=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,model);
	        model_list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	        spin_model.setAdapter(model_list);
	        ArrayAdapter<String> type_list=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,type);
	        type_list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	        spin_type.setAdapter(type_list);
	        
	        
	        vehiclename=(EditText)findViewById(R.id.editText1);
			mileage=(EditText)findViewById(R.id.editText2);
			addvehicle=(Button)findViewById(R.id.button1);
			ctx=this;
			addvehicle.setOnClickListener(this);
			
	 }

	public void onItemSelected(AdapterView<?> arg0, View v, int position,
			long id) {
		
	 
		if (id==R.id.spinner1)
			modelStr=model[position];
		else if(id==R.id.spinner2)
			typeStr=type[position];
		
	}
	

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public void onBackPressed() {
		// TODO Auto-generated method stub
		finish();
	}
	public void onClick(View v) {
		if (v.getId() == R.id.button1) {
		  
			String vehiclenameStr=vehiclename.getEditableText().toString();
			String mil=mileage.getEditableText().toString();
			SharedPreferences pref = getSharedPreferences(AppConstant.PREFS_NAME,0);
			String uName = pref.getString("username", null);
			addvehicle(uName,vehiclenameStr,modelStr,mil,typeStr);
		}
	}
	
	
	public void addvehicle(String uname, String vehiclename, String model,String mileage, String type) {
		System.out.println("vehiclename="+vehiclename);
		System.out.println("model="+model);
		System.out.println("mileage="+mileage);

		String Addvehicle=AppConstant.ADD_VEHICLE+"?emailid="+uname+"&vhname="+vehiclename+"&model="+model+"&mil="+mileage+"&type="+type+"&method=add";
		System.out.println("Addvehicle="+Addvehicle);
		HttpASync async = new HttpASync();
		async.setOnRequestListener(this);
		async.execute(Addvehicle);
			
		}

	@Override
	public void onResponseReceived(String response)
	{
		System.out.println("respStr = "+response);
		if(response.contains("Added")) {
			Toast.makeText(ctx,"Vehicle added successfully",Toast.LENGTH_SHORT).show();
			finish();
		}
		else {
			Toast.makeText(ctx, response, Toast.LENGTH_SHORT).show();
		}
	
	}

	
}
